package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @Author lvFF
 * @Date 2022/04/25/下午 3:39;
 * Description
 */
@RestController
@RequestMapping("employee")
@Slf4j
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 登入界面
     * @param employee
     * @param request
     * @return R
     */
    @PostMapping("login")
    public R<Employee> login(@RequestBody Employee employee,
                             HttpServletRequest request){
        String username = employee.getUsername();
        String password = employee.getPassword();

        //将页面提交的密码password进行md5加密
       password =  DigestUtils.md5DigestAsHex(password.getBytes());
       //根据提交的userName 进行查询
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        Employee one = employeeService.getOne(queryWrapper);
        if (one==null){
            return R.error("用户不存在");
        }
        String password1 = one.getPassword();
        //用户存在对比密码
        if (!(password.equals(password1))){
            return R.error("密码错误");
        }
        //
        //密码正确的情况下 比较用户状态
        Integer status = one.getStatus();
        if (status == 0){
            return R.error("员工账号已经被禁用");
        }
        //登入成果,响应给前端
        request.getSession().setAttribute("Employee",one.getId());
        return  R.success(one);
    }

    /**
     * 退出管理主界面
     * @param request
     * @return
     */
    @PostMapping("logout")
    public R<String> logout(HttpServletRequest request){
        //销毁Session
        log.info("退出按钮---退出成果");
        request.getSession().invalidate();
        return R.success("退出成果");
    }

    /**
     * 添加员工信息
     * @param employee
     * @param request
     * @return R<employee>
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpServletRequest request){
        log.info("员工增加信息：---------");
        //设置初始密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //
        Long employee1 = (long)request.getSession().getAttribute("Employee");
        employee.setCreateUser(employee1);
        employee.setUpdateUser(employee1);
        employeeService.save(employee);
        return R.success("增加成果");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page<Employee>> page(Integer page,Integer pageSize,String name){
       /* Page<Employee> employeePage = new Page<>();
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();

        Page<Employee> page1 = employeeService.page(employeePage, queryWrapper);*/
        log.info("分页查询------");
        Page<Employee> employeePage = new Page<>(page,pageSize);
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        //
        queryWrapper.like(StringUtils.isNotBlank(name),"name",name);
        //
        Page<Employee> page1 = employeeService.page(employeePage, queryWrapper);
        return R.success(page1);
    }

    /**
     * 修改
     * @param employee
     * @param request
     * @return
     */
    @PutMapping
    public R forbidden(@RequestBody Employee employee, HttpServletRequest request){
        Long id = (long)request.getSession().getAttribute("Employee");
        log.info("id----{}",id);
        //
        employee.setUpdateTime(LocalDateTime.now());
        //
        employee.setUpdateUser(id);
       // employee.setStatus(1);
        employeeService.updateById(employee);
        return R.success("修改成果");
    }

    /**
     * 根据id修改与启用和禁用
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<Employee>  getById(@PathVariable Long id){
        log.info("传进来的ID为-----{}",id);
        Employee byId = employeeService.getById(id);
        log.info("转换的id为-------{}",byId);
        return R.success(byId);
    }
}
